package message

import (
	"npxy-go/pkg/tcp"
	"npxy-go/pkg/tcp/handlers"
)

type Type int8

const (
	CONNECTION Type = iota
	DISCONNECTION
	FORWARD
	CONFIG
)

const HeadSize = 5
const TotalHeadSize = HeadSize + handlers.ProtocolHeadSize

type Message interface {
	Decode(buf *tcp.Buffer)
	Encode(buf *tcp.Buffer)
}

func Decode(buf *tcp.Buffer) Message {
	tp := Type(buf.ReadInt8())
	var msg Message
	switch tp {
	case CONNECTION:
		msg = &ConnectionMessage{}
	case DISCONNECTION:
		msg = &DisconnectionMessage{}
	case FORWARD:
		msg = &ForwardMessage{}
	case CONFIG:
		msg = &ConfigMessage{}
	}
	msg.Decode(buf)
	return msg
}
